package com.cvpoc.hibernate.dao.impl;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.BeanUtils;
import org.springframework.orm.hibernate3.HibernateObjectRetrievalFailureException;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.cvpoc.dto.ExperienceCompetenceDTO;
import com.cvpoc.dto.ExperienceDTO;
import com.cvpoc.hibernate.dao.ExperienceDao;
import com.cvpoc.model.Experience;
import com.cvpoc.model.ExperienceCompetence;

public class ExperienceDaoImpl extends HibernateDaoSupport implements
		ExperienceDao {

	@Override
	public Integer createExperience(ExperienceDTO pExperienceDTO) {
		Experience experience = getExperience(pExperienceDTO);
		return (Integer) getHibernateTemplate().save(experience);
	}

	@Override
	public ExperienceDTO findExperienceById(int pIdExperience) {
		try {
			return getExperienceDTO(getHibernateTemplate().get(
					Experience.class, pIdExperience));
		} catch (HibernateObjectRetrievalFailureException ex) {
			return null;
		}
	}

	@Override
	public void deleteExperience(int pIdExperience) {
		getHibernateTemplate().delete(
				getHibernateTemplate().load(Experience.class,
						Integer.valueOf(pIdExperience)));
	}

	@Override
	public void updateExperience(ExperienceDTO pExperienceDTO) {
		getHibernateTemplate().merge(getExperience(pExperienceDTO));
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<ExperienceDTO> findExperiencesByCollaborateur(
			int idCollaborateur) {
		return getListeExperienceDTO(getHibernateTemplate().find(
				"select e from Experience e where e.id_collaborateur = "
						+ idCollaborateur));

	}

	private static ExperienceDTO getExperienceDTO(Experience pExperience) {
		ExperienceDTO experienceDTO = new ExperienceDTO();
		BeanUtils.copyProperties(pExperience, experienceDTO);
		// Gestion des competences associées
		Set<ExperienceCompetenceDTO> experienceCompetenceDTO = Collections
				.emptySet();

		if (null != pExperience.getExperienceCompetences()) {

			experienceCompetenceDTO = getListeExperienceCompetenceDTO(pExperience
					.getExperienceCompetences());
		}

		experienceDTO.setExperienceCompetences(experienceCompetenceDTO);
		return experienceDTO;
	}

	private static Experience getExperience(ExperienceDTO pExperienceDTO) {
		Experience experience = new Experience();
		BeanUtils.copyProperties(pExperienceDTO, experience,
				new String[] { "experienceCompetences" });
		experience
				.setExperienceCompetences(getListeExperienceCompetence(pExperienceDTO
						.getExperienceCompetences()));
		pExperienceDTO.getId_collaborateur();
		return experience;
	}

	private static List<ExperienceDTO> getListeExperienceDTO(
			List<Experience> pListeExperiences) {
		List<ExperienceDTO> listeExperienceDTO = new ArrayList<ExperienceDTO>();
		for (Experience experience : pListeExperiences) {
			listeExperienceDTO.add(getExperienceDTO(experience));
		}
		return listeExperienceDTO;
	}

	// private static CompetenceDTO getCompetenceDTO(Competence pCompetence) {
	// CompetenceDTO competenceDTO = new CompetenceDTO();
	// BeanUtils.copyProperties(pCompetence, competenceDTO);
	// return competenceDTO;
	// }
	//
	// private static Set<CompetenceDTO> getListeCompetenceDTO(
	// List<Competence> pListeCompetences) {
	// Set<CompetenceDTO> listeCompetenceDTO = new HashSet<CompetenceDTO>();
	// for (Competence competence : pListeCompetences) {
	// listeCompetenceDTO.add(getCompetenceDTO(competence));
	// }
	// return listeCompetenceDTO;
	// }

	private static ExperienceCompetenceDTO getExperienceCompetenceDTO(
			ExperienceCompetence pExperienceCompetence) {
		ExperienceCompetenceDTO experienceCompetenceDTO = new ExperienceCompetenceDTO();
		BeanUtils.copyProperties(pExperienceCompetence,
				experienceCompetenceDTO, new String[] { "competence" });
		experienceCompetenceDTO.setCompetence(CompetenceDaoImpl
				.getCompetenceDTO(pExperienceCompetence.getCompetence()));

		return experienceCompetenceDTO;
	}

	private static Set<ExperienceCompetenceDTO> getListeExperienceCompetenceDTO(
			Collection<ExperienceCompetence> pListeExperienceCompetences) {
		Set<ExperienceCompetenceDTO> listeExperienceCompetenceDTO = new HashSet<ExperienceCompetenceDTO>();
		for (ExperienceCompetence experienceCompetence : pListeExperienceCompetences) {
			listeExperienceCompetenceDTO
					.add(getExperienceCompetenceDTO(experienceCompetence));
		}
		return listeExperienceCompetenceDTO;
	}

	private static ExperienceCompetence getExperienceCompetence(
			ExperienceCompetenceDTO pExperienceCompetenceDTO) {
		ExperienceCompetence experienceCompetence = new ExperienceCompetence();
		BeanUtils.copyProperties(pExperienceCompetenceDTO,
				experienceCompetence, new String[] { "competence" });
		experienceCompetence.setCompetence(CompetenceDaoImpl
				.getCompetence(pExperienceCompetenceDTO.getCompetence()));

		return experienceCompetence;
	}

	private static Set<ExperienceCompetence> getListeExperienceCompetence(
			Set<ExperienceCompetenceDTO> pListeExperienceCompetencesDTO) {
		Set<ExperienceCompetence> listeExperienceCompetence = new HashSet<ExperienceCompetence>();
		if (pListeExperienceCompetencesDTO != null) {
			for (ExperienceCompetenceDTO experienceCompetenceDTO : pListeExperienceCompetencesDTO) {
				listeExperienceCompetence
						.add(getExperienceCompetence(experienceCompetenceDTO));
			}
		}
		return listeExperienceCompetence;
	}

	@Override
	public void deleteExperienceCompetence(
			ExperienceCompetenceDTO pExperienceCompetenceDTO) {
		getHibernateTemplate().delete(
				getExperienceCompetence(pExperienceCompetenceDTO));
	}

	@Override
	public void createExperienceCompetence(
			ExperienceCompetenceDTO pExperienceCompetenceDTO) {
		getHibernateTemplate().save(
				getExperienceCompetence(pExperienceCompetenceDTO));
	}

	@SuppressWarnings("unchecked")
	@Override
	public Set<ExperienceCompetenceDTO> findListExperienceCompetenceByIdExperience(
			int pIdExperience) {
		DetachedCriteria dc = DetachedCriteria
				.forClass(ExperienceCompetence.class);
		dc.add(Restrictions.eq("id_experience", pIdExperience));
		return getListeExperienceCompetenceDTO(getHibernateTemplate()
				.findByCriteria(dc));
	}

	@Override
	public void updateExperienceCompetence(
			ExperienceCompetenceDTO pExperienceCompetenceDTO) {
		getHibernateTemplate().merge(
				getExperienceCompetence(pExperienceCompetenceDTO));
	}

}
